package wtws.manager.in;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import wtws.manager.in.interf.ProductDecoder;
import wtws.message.PrecipitationProduct;

public class PrecipitationDecoder implements ProductDecoder {

	public static PrecipitationProduct processFile(File file) throws FileNotFoundException, ParseException {
		int lnNumOfLine = 0;
		long llTimestamp = 0L;
		float elevation = (float) 0.6;
		ArrayList<Short> shortList = new ArrayList<Short>();

		Scanner fin = new Scanner(file);
		while (fin.hasNextLine()) {
			String line = fin.nextLine().trim();
			lnNumOfLine++;
			if (lnNumOfLine == 1) {
				Date date = new SimpleDateFormat("yyyyMMddHHmmss").parse(line.substring(line.length() - 14));
				llTimestamp = date.getTime() / 1000 + 8 * 60 * 60;
			} else if (lnNumOfLine == 2) {
				//elevation = Float.parseFloat(line.split(":")[1].trim());
			} else if (lnNumOfLine == 3) {
				;
			} else {
				for (String num : line.split(",")) {
					Short x = Short.parseShort(num.trim());
					shortList.add(x);
				}
			}
		}
		fin.close();
		return new PrecipitationProduct(1, elevation, llTimestamp, shortList);

	}

}
